package com.ist.neartweat.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Hashtable;

import com.ist.neartweat.messages.BroadCastMessage;
import com.ist.neartweat.messages.ByeMessage;
import com.ist.neartweat.messages.Message;
import com.ist.neartweat.messages.PrivateMessage;
import com.ist.neartweat.messages.RegistartionSuccessMessage;
import com.ist.neartweat.messages.RegistrationMessage;
import com.ist.neartweat.platForm.Reply;
import com.ist.neartweat.platForm.Tweat;

public class NearTweatServer{
	private Hashtable<String,Socket> clientHandles =  new Hashtable<String,Socket>();
	private Hashtable<String,ArrayList<Tweat>> tweats = new  Hashtable<String,ArrayList<Tweat>>();//Tweat Owner and list of tweats made by that user.
	private Hashtable<String,ObjectOutputStream> clientOutStreams = new Hashtable<String,ObjectOutputStream>();
	public static void main(String args[]){
		try {
			int i=0;
			
			ServerSocket server = new ServerSocket(9876);
			NearTweatServer tweatServer =  new NearTweatServer();
			System.out.println("Succefully created the server");
			/*For Testing */
			ArrayList<Tweat> test1 = new ArrayList<Tweat>();
			ArrayList<Tweat> test2 = new ArrayList<Tweat>();
			for(i=0;i<15;i++){
				test1.add(new Tweat("raj","raj:"+i,"TweatString from tweatId"+i));
				test2.add(new Tweat("max","max:"+i,"TweatString from tweatId"+i));
				
			}
			ArrayList<Tweat> test3 = new ArrayList<Tweat>();
			Tweat ty = new Tweat("raj2","raj2:1","TweatString from tweatId raj2");
			ty.addPubReply(new Reply("raj","raj2","This is fisrt reply",System.currentTimeMillis()));
			ty.addPubReply(new Reply("raj","raj2","This is second reply",System.currentTimeMillis()));
			test3.add(ty);
			tweatServer.tweats.put("raj",test1);
			tweatServer.tweats.put("max",test2);
			tweatServer.tweats.put("raj2",test3);
			/* End of for testing */
			
			Socket clientSocket;
			//Accept Connections
			while(true){
				clientSocket = server.accept();
				new ClientHandlerThread(tweatServer,clientSocket).start();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Failed to create the server at port 9876");
			e.printStackTrace();
		}
	}

	public Boolean addClient(String clientId, Socket clientSocket) {
		if(clientHandles.containsKey(clientId)){
			return false;
		}
		else{
			clientHandles.put(clientId, clientSocket);
			return true;
		}
	}
	
	public void addOutStream(String clientId, ObjectOutputStream ops){
		clientOutStreams.put(clientId,ops);
	}
	
		
	public void handleMessage(Message msg) throws CloneNotSupportedException {
		if(msg instanceof PrivateMessage){
			
		}
		else if(msg instanceof BroadCastMessage){
			
		}
		else if(msg instanceof ByeMessage){
			
		}
		else if(msg instanceof RegistrationMessage){
			//We will come here only when registration is successful in ClientHandlerThread class's run method.
			//send RegistrationSuccessMessage which contains tweats
			String clientId = ((RegistrationMessage)msg).getClientId();
			RegistartionSuccessMessage rms = new RegistartionSuccessMessage();
			ArrayList<Tweat> toSend = new ArrayList<Tweat>();
			for(String s: tweats.keySet()){
				for(Tweat t : tweats.get(s)){
					toSend.add(t.clone(clientId));
				}
			}
			rms.setTweats(toSend);
			try {
				clientOutStreams.get(clientId).writeObject(rms);
				System.out.println("Sending the tweats" + "tweat size is "+rms.getTweats().size());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}